@use 'sass:map';
@import 'mixins/size';

@include b(radio) {
  position: relative;
  display: inline-flex;
  line-height: 1.25;

  &:hover {
    @include e(input) {
      border-color: var(--#{$rd-prefix}color-primary-lighter);
    }
  }

  @include component-size;

  @include when(disabled) {
    color: var(--#{$rd-prefix}color-disabled);
    pointer-events: none;

    @include e(input-wrapper) {
      background-color: var(--#{$rd-prefix}background-color-disabled);

      &::after {
        background-color: var(--#{$rd-prefix}color-disabled);
      }
    }
  }

  @include when(checked) {
    &:not(.is-disabled) {
      @include e(input) {
        border-color: var(--#{$rd-prefix}color-primary);
      }

      @include e(input-wrapper) {
        &::before {
          animation: wave-spread 0.36s linear;
          animation-fill-mode: backwards;
        }
      }
    }

    @include e(input-wrapper) {
      &::after {
        opacity: 1;
        transform: scale(1);
      }
    }
  }

  @include m(button) {
    height: var(--#{$rd-prefix}size);
    white-space: nowrap;
    cursor: pointer;
    border: 1px solid var(--#{$rd-prefix}color-border);
    border-radius: var(--#{$rd-prefix}border-radius);
    transition: border-color var(--#{$rd-prefix}animation-duration-base) linear,
      background-color var(--#{$rd-prefix}animation-duration-base) linear;

    @include when(disabled) {
      background-color: var(--#{$rd-prefix}background-color-disabled);

      @include when(checked) {
        color: rgb(var(--#{$rd-prefix}color-primary-rgb) / 50%);
      }
    }

    @include when(focus-visible) {
      border-color: var(--#{$rd-prefix}color-primary-lighter);
      box-shadow: 0 0 0 3px rgb(var(--#{$rd-prefix}color-primary-rgb) / var(--#{$rd-prefix}shadow-normal));
    }

    @include e(input-wrapper) {
      width: 0;
      height: 0;
      margin-right: 0;
      overflow: hidden;
    }

    @include e(label) {
      display: inline-flex;
      align-items: center;
      height: 100%;
      padding: 0 calc(var(--#{$rd-prefix}padding-size) - 1px);
      transition: color var(--#{$rd-prefix}animation-duration-base) linear;
    }
  }

  @include m(button-outline) {
    &:hover {
      color: var(--#{$rd-prefix}color-primary-lighter);
      border-color: var(--#{$rd-prefix}color-primary-lighter);
    }

    @include when(checked) {
      color: var(--#{$rd-prefix}color-primary);
      border-color: var(--#{$rd-prefix}color-primary);
    }
  }

  @include m(button-fill) {
    background-color: var(--#{$rd-prefix}background-color);

    &:hover {
      color: var(--#{$rd-prefix}color-primary-lighter);
      border-color: var(--#{$rd-prefix}color-primary-lighter);
    }

    @include when(checked) {
      color: map.get($rd-colors, 'white');
      background-color: var(--#{$rd-prefix}color-primary);
      border-color: var(--#{$rd-prefix}color-primary);
    }
  }

  @include e(input-wrapper-wrapper) {
    display: inline-flex;
    flex-shrink: 0;
    align-items: center;
    height: 1.25em;
  }

  @include e(input-wrapper) {
    position: relative;
    width: 16px;
    height: 16px;
    margin-right: 8px;
    cursor: pointer;
    background-color: var(--#{$rd-prefix}background-color-input);
    border-radius: 50%;

    &::before {
      position: absolute;
      top: 0;
      right: 0;
      bottom: 0;
      left: 0;
      content: '';
      border: 1px solid var(--#{$rd-prefix}color-primary);
      border-radius: 50%;
      opacity: 0;
    }

    &::after {
      position: absolute;
      top: 4px;
      right: 4px;
      bottom: 4px;
      left: 4px;
      content: '';
      background-color: var(--#{$rd-prefix}color-primary);
      border-radius: 50%;
      opacity: 0;
      transition: transform var(--#{$rd-prefix}animation-duration-fast) linear, opacity var(--#{$rd-prefix}animation-duration-fast) linear;
      transform: scale(0);
    }
  }

  @include e(input) {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: 1;
    width: 100%;
    height: 100%;
    margin: 0;
    cursor: inherit;
    border: 1px solid var(--#{$rd-prefix}color-border);
    border-radius: 50%;
    outline: none;
    transition: border-color var(--#{$rd-prefix}animation-duration-base) linear;
    appearance: none;

    &:focus {
      border-color: var(--#{$rd-prefix}color-primary-lighter);
      box-shadow: 0 0 0 3px rgb(var(--#{$rd-prefix}color-primary-rgb) / var(--#{$rd-prefix}shadow-normal));
    }
  }
}

@include b(radio-group) {
  display: flex;
  flex-wrap: wrap;
  align-items: center;

  @include when(change) {
    .#{$rd-prefix}radio,
    .#{$rd-prefix}radio__label {
      transition: none;
    }
  }

  @include m(vertical) {
    align-items: flex-start;
  }

  @include m(default) {
    &:not(.#{$rd-prefix}radio-group--vertical) {
      @include polyfill-gap(8px, 12px);
    }
  }

  @include m(vertical) {
    &.#{$rd-prefix}radio-group--default {
      @include polyfill-row-gap(8px);

      flex-direction: column;
    }
  }
}
